<template>
    <el-select v-model="sltValue" class="w-full" clearable placeholder="请选择所属分组"
        @focus="handleFocus" @change="doSelectChange">
        <el-option v-for="item in options" :key="item.deptId" :label="item.deptName"
            :value="item.deptId" />
    </el-select>
</template>

<script setup>
import { ref, watch, getCurrentInstance } from 'vue'
import { getDeptList } from '@/api/clients/signClient.ts'

const { emit, appContext: { app: { config: { globalProperties } } } } = getCurrentInstance()

const props = defineProps({
    value: {
        type: [String, Number]
    }
})

const sltValue = ref('')

const emits = defineEmits(['update:value', 'change'])
const options = ref([])

const handleFocus = () => {
    if(options.value?.length) return
    getDeptList({
        platform: 2,
        type: 1
    }).then(res => {
        options.value = res

        // if (globalProperties.roleflag === 2) {
        //     options.value = res
        //     // options.value.push({
        //     //     deptId: 1,
        //     //     deptName: '铁道部'
        //     // })
        // } else if(globalProperties.roleflag === 1) {
        //     const deptId = sessionStorage.getItem('deptId')
        //     options.value = res.filter(i => i.deptId === deptId)
        // }
    })
}

const doSelectChange = () => {
    emits('update:value', sltValue.value)
    emits('change')
}

watch(() => props.value, (newval) => {
    sltValue.value = newval
}, {
    immediate: true
})
</script>
<style lang="scss" scoped>
    
</style>